import Taro from '@tarojs/taro';
import u from 'updeep'
import request from '../util/request'

export default {
  namespace: 'companyRelease',
  state: {

    districtTypeRange: [],
    // 条件
    districtType: {
      id: 0,
      name: '不限'
    },
    categoryTypeRange:[],
    categoryType:{
      id:0,
      typeName:'不限'
    },
    topImage:'',
    businessName:'',
    businessContent:'',
    position:'',
    location:'',
    contact: "",
    cellphone: "",
    description: "",
    title: "",
    isOpened: false,
    fileList:[],
    imgloading: true,
    certificates: [], // 资质证书
    partners: [], // 合作客户
    others: [], // 企业形象
    employees: [], // 员工风采
  },
  reducers: {
    updateState(state, {payload}) {
      return u({
        ...payload
      }, state)
    },
    initData(state, action) {
      return {
        ...state,
        ...action.payload,
        districtType: {
          id: 0,
          name: '不限'
        },
        contact: "",
        cellphone: "",
        title: "",
        description: "",
        endData: "",
      }
    }
  },
  effects: {
    * loadUserBusiness({payload = {}}, {put, select,call}){
      const {
        districtTypeRange
      } = yield select(({companyRelease}) => companyRelease)
      const {data} = yield call(request, {
        url: SERVERURL + '/service/business/me',
        method: 'get',
      })
      if (data && data === "unauthc") {
        Taro.navigateTo({
          url: `/pages/login/index`
        })
        Taro.showToast({
          title: ("会话过时"),
          icon: 'none'
        })
      }

      if (data && data.code==='000000' && data.data.id !==null){
        const d = data.data || {};
        yield put({
          type: 'updateState',
          payload: {
            topImage:d.topImage,
            businessName:d.businessName,
            businessContent:d.businessContent,
            position:d.position,
            location:d.location,
            contact: d.contact,
            cellphone: d.cellphone,
            description: d.description,
            certificates: (d.certificates||[]).map(item => {return {url: item}}),
            partners: (d.partners||[]).map(item => {return {url: item}}),
            others: (d.others||[]).map(item => {return {url: item}}),
            employees: (d.employees||[]).map(item => {return {url: item}}),
            title: d.title,
            fileList:d.topImage.split(','),
            districtType:{
              id:  d.districtId,
              name: d.districtName
            },
            categoryType: {
              id:d.typeId,
              typeName:d.typeName
            }
          }
        })
      }
    },

    * loadType({payload = {}}, {put, call}) {
      const {data} = yield call(request, {
        url: SERVERURL + '/service/type/types',
        method: 'post',
        data:{
          category:"business"
        }
      })
      if (data && data.data instanceof Array) {
        const d = data.data || {};
        yield put({
          type: 'updateState',
          payload: {
            categoryTypeRange: d
          }
        })
      } else {
        Taro.showToast({
          title: (data || {}).msg || '获取列表数据失败',
          icon: 'none'
        })
      }
    },

    // 获得区域信息
    * loadDistrict({payload = {}}, {put, call}) {
      const {data} = yield call(request, {
        url: SERVERURL + '/service/city/all',
        method: 'get',
      })

      if (data && data.data instanceof Array) {
        const d = data.data || {};
        yield put({
          type: 'updateState',
          payload: {
            districtTypeRange: d
          }
        })
      } else {
        Taro.showToast({
          title: (data || {}).msg || '获取列表数据失败',
          icon: 'none'
        })
      }
    },
    //添加
    * addOrder({payload = {}}, {select, call}) {
      const {
        businessName, businessContent, districtType, contact,
        position, location, cellphone, title, description,categoryType,
        endData, fileList,
        certificates, partners, others, employees
      } = yield select(({companyRelease}) => companyRelease)
      let districtId = districtType.id;

      let fileUpList = [];
      if (fileList.length>4){

        for(let i=0;i<4;i++){
          fileUpList.push(fileList[i])
        }

      }else{
        fileUpList = fileList;
      }

      let fileString = fileUpList.join(",")

      if (contact == "" || cellphone == "" || description == ""
        || businessName == "" || businessContent == "" ) {
        Taro.showToast({
          title: "信息未填写完整！",
          icon: 'none'
        })
        return false
      }
      if(!(/^1[3456789]\d{9}$/.test(cellphone))){
        Taro.showToast({
              title: "手机号码有误，请重填！",
              icon: 'none'
            })
        return false;
    }
      const param = {
        topImage:fileString,
        businessName:businessName,
        businessContent:businessContent,
        contact:contact,
        position:position,
        location:location,
        description:description,
        districtId:districtId,
        typeId:categoryType.id,
        cellphone: cellphone,
        certificates: certificates.map(item => item.url),
        partners: partners.map(item => item.url),
        others: others.map(item => item.url),
        employees: employees.map(item => item.url),
      }
      let url = 'service/business/save';

      const {data} = yield call(request, {
        url: SERVERURL + url,
        data: param,
        method: 'post',
      })

      if (data && data === "unauthc") {
        Taro.navigateTo({
          url: `/pages/login/index`
        })
        Taro.showToast({
          title: ("会话过时"),
          icon: 'none'
        })
      }

      if (data.code === '000000') {
        Taro.showToast({
          title: (data || {}).msg || '保存成功',
          icon: 'none'
        })
        // Taro.navigateBack()
      }else{
        Taro.showToast({
          title: (data || {}).msg || '保存失败',
          icon: 'none'
        })
      }
    }
  }
};
